package com.xb.loan.cashier.biz.service.impl;

import com.xb.loan.cashier.biz.dao.CashierPayChannelBankDao;
import com.xb.loan.cashier.biz.dao.bean.CashierPayChannelBank;
import com.xb.loan.cashier.biz.service.CacheService;
import com.xb.loan.cashier.biz.service.CashierPayChannelBankService;
import com.xb.loan.cashier.biz.service.cache.RedisMethodCacheSupport;
import com.xb.loan.util.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("cashierPayChannelBankService")
public class CashierPayChannelBankServiceImpl implements CashierPayChannelBankService,CacheService {

    @Autowired
    private CashierPayChannelBankDao cashierPayChannelBankDao;
    
    @Override
    public List<String> getAllBankCodePlat() {
        List<String> bankCodes = RedisMethodCacheSupport.getBeans(String.class);
        if(CollectionUtils.isEmpty(bankCodes)){
            bankCodes = cashierPayChannelBankDao.getAllBankCodePlat();
            if(CollectionUtils.isNotEmpty(bankCodes)){
                RedisMethodCacheSupport.setBeans(String.class, bankCodes);
            }
        }
        return bankCodes;
    }

    @Override
    public List<CashierPayChannelBank> getAllPayChannelBanks() {
        List<CashierPayChannelBank> banks = RedisMethodCacheSupport.getBeans(CashierPayChannelBank.class);
        if(CollectionUtils.isEmpty(banks)){
            banks = cashierPayChannelBankDao.getAllPayChannelBanks();
            if(CollectionUtils.isNotEmpty(banks)){
                RedisMethodCacheSupport.setBeans(CashierPayChannelBank.class, banks);
            }
        }
        return banks;
    }
    
    @Override
    public CashierPayChannelBank getByBankCodePlatAndType(int payChannelType, String bankCodePlat){
        String[] keys = new String[]{payChannelType+"", bankCodePlat};
        CashierPayChannelBank bank = RedisMethodCacheSupport.getBean(CashierPayChannelBank.class, keys);
        if(bank==null){
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("paychannelType", payChannelType);
            params.put("bankCodePlat", bankCodePlat);
            bank = cashierPayChannelBankDao.getByBankCodePlatAndType(params);
            if(bank!=null){
                RedisMethodCacheSupport.setBean(CashierPayChannelBank.class, bank, keys);
            }
        }
        return bank;
    }

    @Override
    public List<CashierPayChannelBank> getByBankCodePlat(String bankCodePlat) {
        List<CashierPayChannelBank> banks = RedisMethodCacheSupport.getBeans(CashierPayChannelBank.class, bankCodePlat);
        if(CollectionUtils.isEmpty(banks)){
            banks = cashierPayChannelBankDao.getByBankCodePlat(bankCodePlat);
            if(CollectionUtils.isNotEmpty(banks)){
                RedisMethodCacheSupport.setBeans(CashierPayChannelBank.class, banks, bankCodePlat);
            }
        }
        return banks;
    }

    @Override
    public void clearCache(String methodName) {
        RedisMethodCacheSupport.clear(methodName);
    }

    @Override
    public void clearCacheAll() {
        RedisMethodCacheSupport.clearAll();
    }

	@Override
	public List<CashierPayChannelBank> queryPayChannelBanksByPage(
            Integer pageNum, Integer pageSize, String bankCode) {
		String[] keys = new String[]{pageNum+"",pageSize+"",bankCode};
		List<CashierPayChannelBank> banks = RedisMethodCacheSupport.getBeans(CashierPayChannelBank.class,keys);
        if(CollectionUtils.isEmpty(banks)){
        	Map<String, Object> params = new HashMap<String, Object>();
        	params.put("pageNum", pageNum);
    		params.put("pageSize", pageSize);
    		params.put("bankCode", bankCode);
            banks = cashierPayChannelBankDao.queryPayChannelBankByPage(params);
            if(CollectionUtils.isNotEmpty(banks)){
                RedisMethodCacheSupport.setBeans(CashierPayChannelBank.class, banks,keys);
            }
        }
        return banks;
	}

	@Override
	public String queryBankNameByCode(String bankCode) {
		 String bankName = RedisMethodCacheSupport.getBean(String.class, bankCode);
	        if(StringUtils.isEmpty(bankName)){
	        	bankName = cashierPayChannelBankDao.queryBankNameByCode(bankCode);
	            if(StringUtils.isNotEmpty(bankName)){
	                RedisMethodCacheSupport.setBean(String.class, bankName,bankCode);
	            }
	        }
	        return bankName;
	}

}
